package com.xingzhe.learning.service.Impl;

import com.alibaba.fastjson.JSON;
import com.xingzhe.base.exception.XingZhePlusException;
import com.xingzhe.learning.config.PayNotifyConfig;
import com.xingzhe.learning.service.MyCourseTablesService;
import com.xingzhe.messagesdk.model.po.MqMessage;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 接受消息后通知处理类
 */
@Slf4j
@Service
public class ReceivePayNotifyService {
    @Resource
    MyCourseTablesService myCourseTablesService;
    @RabbitListener(queues = PayNotifyConfig.PAYNOTIFY_QUEUE)
    public void receive(Message message){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //解析出消息
        byte[] body = message.getBody();
        String JsonString = new String(body);
        //转为对象
        MqMessage mqMessage = JSON.parseObject(JsonString, MqMessage.class);

        //解析消息的内容
        //chooseCourseId  选课id
        String chooseCourseId = mqMessage.getBusinessKey1();
        //订单类型
        String orderType = mqMessage.getBusinessKey2();
        //学习中心服务看是不是购买课程类的支付订单的消息
        if (orderType.equals("60201")) {
            //根据消息内容更新选课记录，向我的课程表插入记录
            boolean b = myCourseTablesService.saveChooseCourseSuccess(chooseCourseId);
            if (!b){
                XingZhePlusException.cast("保存选课记录状态失败");
            }
        }
    }
}
